<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午12:03
 *
 * 用户财务资金类
 */

use LaravelBook\Ardent\Ardent;

class MemberAccount extends Ardent{

    protected $table = 'member_accounts';

    public static $relationsData = array(
        'member' => array(self::BELONGS_TO, 'Member', 'foreignKey' => 'member_id', 'otherKey' => 'id'),
    );

    //账户类型
    const INTEGRAL_ACCOUNT = 'INTEGRAL_ACCOUNT';
    const SCHOOL_CARD_ACCOUNT = 'SCHOOL_CARD_ACCOUNT';
    const WEIXIN_ACCOUNT = 'WEIXIN_ACCOUNT';

    public static $accountTypes = array(
        array('id' => 0,'keyword'=>'INTEGRAL_ACCOUNT',   'display_name'=>'积分账户'),
        array('id' => 1,'keyword'=>'SCHOOL_CARD_ACCOUNT',   'display_name'=>'校园卡账户'),
        array('id' => 2,'keyword'=>'WEIXIN_ACCOUNT',   'display_name'=>'微信账户'),
    );

    public static $column = array(
        'member_id' => '用户昵称',
        'account_type' => '账户类型',
        'current_money' => '当前余额(元)',
        'created_at' => '开户时间',
    );

    public static function getAccountTypeIDByKeyword($keyword){
        foreach(self::$accountTypes as $accountType){
            if($accountType['keyword'] == $keyword){
                return $accountType['id'];
            }
        }
        return false;
    }

    public function beforeDelete( $forced = false )
    {
        $this->errors()->add('error', '该表不允许删除信息');
        return false;
    }

    /**
     * 新会员开通账户
     * @param $memberId 会员ID
     * @param string $accountType 账户类型：默认积分账户
     * @return boolean
     */
    public function getAccount($memberId, $accountType = self::INTEGRAL_ACCOUNT){
        $account = MemberAccount::where('member_id', $memberId)->where('account_type', MemberAccount::getAccountTypeIDByKeyword($accountType))->first();
        if(!$account){
            $account = new MemberAccount();
            $account->member_id = $memberId;
            $account->account_type = MemberAccount::getAccountTypeIDByKeyword($accountType);
            $account->current_money = 0.00;

            if(!$account->save()){
                return false;
            }
        }
        return $account;
    }

    /**
     * 会员账户入账
     *
     * @param $money
     * @param string $accountType
     * @param int $orderId
     * @param string $description
     * @return $this
     * @throws Exception
     */
    public function income($money, $accountType = self::INTEGRAL_ACCOUNT, $orderId = 1, $description = ''){
        $accountTypeId = self::getAccountTypeIDByKeyword($accountType);
        //只有积分账户会存入会员积分账户，其他类型不支持
        switch($accountType){
            case self::INTEGRAL_ACCOUNT:
                //存入金额
                $this->current_money = bcadd($this->current_money, $money, 2);
                if(!$this->save()){
                    return $this->errors()->add('error', self::$accountTypes[$accountTypeId]['display_name'].'入账失败！');
                }
                break;
            default:
                break;
        }

        $memberAccountLog = new MemberAccountLog();
        if(!$memberAccountLog->log($this->member_id, $orderId, MemberAccountLog::INCOME, $this->member_id, $accountType, $this->current_money, $money, $description)){
            return $this->errors()->add('error', self::$accountTypes[$accountTypeId]['display_name'].'入账日志记录失败！');
        }
        return true;
    }

    /**
     * 会员账户消费
     *
     * @param $money
     * @param string $accountType
     * @param int $orderId
     * @param string $description
     * @return $this|bool
     */
    public function cost($money, $accountType = self::INTEGRAL_ACCOUNT, $orderId = 1, $description = ''){
        $accountTypeId = self::getAccountTypeIDByKeyword($accountType);
        $memberAccountLog = new MemberAccountLog();

        //只有积分账户会扣除会员积分账户，其他类型不支持
        switch($accountType){
            case self::INTEGRAL_ACCOUNT:

                //判断当前账户是否有足够的款项
                if($this->current_money < $money){
                    $this->errors()->add('error', '支付失败: 您的账户余额不足！');
                }

                //扣除金额
                $this->current_money = bcsub($this->current_money, $money, 2);
                if(!$this->save()){
                    return $this->errors()->add('error', self::$accountTypes[$accountTypeId]['display_name'].'消费失败！');
                }
                break;
            default:

                break;
        }
        if(!$memberAccountLog->log($this->member_id, $orderId, MemberAccountLog::COST, $this->member_id, $accountType, $this->current_money, $money, $description)){
            return $this->errors()->add('error', self::$accountTypes[$accountTypeId]['display_name'].'消费日志记录失败！');
        }
        return true;
    }






}
